package user

import (
	"api/pb/user"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
	return &GetUserListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserListLogic) GetUserList(req *types.UserListRequest) (resp *types.UserListResponse, err error) {
	listResp, err := l.svcCtx.UserRpc.GetUserList(l.ctx, &user.ListRequest{Page: req.Page, PageSize: req.PerPage})
	if err != nil {
		return nil, err
	}

	resp = &types.UserListResponse{
		Total: listResp.Total,
		Pages: listResp.Pages,
		Data:  make([]types.User, len(listResp.Data)),
	}
	for i, u := range listResp.Data {
		resp.Data[i] = types.User{Id: u.Id, Name: u.Name, Password: u.Password}
	}
	return resp, nil
}
